/*****************************************************************************
	File Name:			AIPlayer.cpp
	Authors:			Marc Willis, Trey Turley, Thomas Lee
	Description:		Describes an AI player in the game Tron.
	Date Modified:		08/08/2010
						-File Created
*****************************************************************************/

#include "AIPlayer.h"

/*****************************************************************************
	Function Name:		AIPlayer()
	Description:		Default constructor for the AIPlayer class, creates an
						AIPlayer with default stats
	Input:
		(none)
	Output:
		(none)
*****************************************************************************/
AIPlayer::AIPlayer():Player()
{
	opponent=NULL;
}

/*****************************************************************************
	Function Name:		AIPlayer(int,int,int,int,GameMap&,Player*)
	Description:		Constructor for the AIPlayer class, creates an AIPlayer
						with the given stats
	Input:
		int x			x of the player
		int y			y of the player
		int dir			direction of the player
		int color		color of the player
		GameMap &g		GameMap to be referenced
		Player *p		a pointer to the opponent
	Output:
		(none)
*****************************************************************************/
AIPlayer::AIPlayer(int x,int y,int dir,int color,GameMap &g,Player *p):Player(x,y,dir,color,g)
{
	opponent=p;
}
/*****************************************************************************
	Function Name:		refreshDir()
	Description:		Gets the next direction based on the GameMap the AI is
						on.
	Input:
		(none)
	Output:
		(none)
*****************************************************************************/
void AIPlayer::refreshDir()
{
	int temp=dir;
	if(!gm->isSafe(getNextX(),getNextY()))
	{
		dir=DIR_LEFT;
		if(!gm->isSafe(getNextX(),getNextY()))
		{
			dir=DIR_UP;
			if(!gm->isSafe(getNextX(),getNextY()))
			{
				dir=DIR_RIGHT;
				if(!gm->isSafe(getNextX(),getNextY()))
				{
					dir=DIR_DOWN;
					if(!gm->isSafe(getNextX(),getNextY()))
					{
						dir=temp;
					}
				}
			}
		}
	}
}
/*****************************************************************************
	Function Name:		getNextX()
	Description:		Gets the predicted next x coordinate for the AI
	Input:
		(none)
	Output:
		int				the next x coordinate
*****************************************************************************/
int AIPlayer::getNextX()
{
	if(dir==DIR_LEFT)
	{
		return x-1;
	}
	if(dir==DIR_RIGHT)
	{
		return x+1;
	}
	return x;
}
/*****************************************************************************
	Function Name:		getNextY()
	Description:		Gets the predicted next y coordinate for the AI
	Input:
		(none)
	Output:
		int				the next y coordinate
*****************************************************************************/
int AIPlayer::getNextY()
{
	if(dir==DIR_UP)
	{
		return y-1;
	}
	if(dir==DIR_DOWN)
	{
		return y+1;
	}
	return y;
}
/*****************************************************************************
	Function Name:		setOpponent(Player*)
	Description:		sets the AI's opponent (only used in zero-player mode)
	Input:
		Player *p		a pointer to the opponent
	Output:
		(none)
*****************************************************************************/
void AIPlayer::setOpponent(Player *p)
{
	opponent=p;
}
